package com.liangdemo.web.thread;

import java.util.concurrent.locks.LockSupport;

/**
 * 两个线程交替打印,一个输出字母，一个输出
 * 1A2B3C4D。。。
 * 使用 LockSupport 的方式，控制线程同步
 */
public class Test3 {

    /**
     * 创建两个静态线程
     */
    static Thread t1 = null, t2 = null;


    public static void main(String[] args) {
        String str1 = "123456789";
        String Str2 = "ABCDEFGHRJK";
        String[] array1 = str1.split("");
        String[] array2 = Str2.split("");
        t1 = new Thread(() -> {
            for (String s : array1) {
                System.out.print(s);
                LockSupport.unpark(t2);//叫醒t2线程
                LockSupport.park();//当前线程睡眠
            }
        }, "t1");

        t2 = new Thread(() -> {
            for (String s : array2) {
                LockSupport.park();//当前线程睡眠
                System.out.print(s);
                LockSupport.unpark(t1);//叫醒t1线程
            }
        }, "t2");
        Test3 test3 = new Test3();
        t1.start();
        t2.start();
    }
}
